<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/17
 * Time: 16:53
 */

namespace App\Console\Service\IPProxyPool\Uitl;


class Proxy
{
    private $proxy_ip;
    private $proxy_port;
    private $check_url;
    private $time_out;
    private $retry;

    public function __construct( $proxy_ip, $proxy_port, $check_url = 'http://www.baidu.com/robots.txt', $time_out = 5, $retry = 2) {
        $this -> proxy_ip = $proxy_ip;
        $this -> proxy_port = $proxy_port;
        $this -> check_url = $check_url;
        $this -> time_out = $time_out;
        $this -> retry = $retry;
    }

    public function check_proxy() {
        // 创建一个新cURL资源
        $ch = curl_init();
        // 设置URL和相应的选项
        curl_setopt($ch, CURLOPT_URL, $this -> check_url);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this -> time_out);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_PROXY, $this -> proxy_ip.':'.$this -> proxy_port);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        // 抓取URL并把它传递给浏览器
        $i = 1;
        $result = false;
        while($i <= $this -> retry) {
            $result = curl_exec($ch);
            if( $result !== false && substr_count($result, 'User-agent: Baiduspider') >=1 ) {
                $result = true;
                break;
            } else {
                $result = false;
            }
            ++$i;
        }

        // 关闭cURL资源，并且释放系统资源
        curl_close($ch);
        //成功返回boolean true, 失败返回boolean false
        return $result;
    }
}
?>